<?php

namespace Application\Sonata\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{

    public function dashboardAction( $licensee_number, $slug )
    {
        $licensee = $this->getDoctrine()
                         ->getRepository('TableTennisLicenseeBundle:Licensee')
                         ->getCurrentLicensee( $licensee_number, $slug )
                         ->getSingleResult();

        $licenseeMatch_list = $this->getDoctrine()
                               ->getRepository('TableTennisLicenseeBundle:LicenseeMatch')
                               ->getLicenseeMatchs( $licensee[ "id" ] )
                               ->getArrayResult();

        $licenseePoint_list = $this->getDoctrine()
                                   ->getRepository('TableTennisLicenseeBundle:LicenseePoint')
                                   ->getLicenseePoints( $licensee[ "id" ] )
                                   ->getArrayResult();

        $licenseePoint_list = array_reverse( $licenseePoint_list );

        /* Graph Data */
        $graphData = array();
        $graphCategories = array();
        
        foreach($licenseePoint_list as $licenseePoint){

            if(!empty($licenseePoint[ 'nb_points_fftt' ])){
                $graphData[] = intval($licenseePoint[ 'nb_points_fftt' ]);
            }else{
                $graphData[] = end($graphData);
            }

            $datetime_points = new \DateTime();
            $month = date('m', strtotime($licenseePoint[ 'datetime_points' ])) - 1;
            $year = date('Y', strtotime($licenseePoint[ 'datetime_points' ]));
            if($month < 1){
                $month = 12;
                $year--;
            }
            $datetime_points->setDate($year, $month, '01');
            $datetime_points->setTime(0, 0, 0);

            $graphCategories[] = (string) date("M", $datetime_points->getTimestamp()) . ".";
        }

        $mensualPoint = "-";
        $evoMensualPoint = "-";
        $startSeasonMensualPoint = "-";

        $mensualPoint = $licensee[ "nb_current_points" ];
        $evoMensualPoint = $licensee[ "monthly_increase" ];
        $startSeasonMensualPoint = isset( $graphData[0] ) ? $graphData[0] : NULL;

        /* Breadcrumbs */
        $breadcrumbs = $this->get( 'white_october_breadcrumbs' );
        $breadcrumbs->addItem( 'Accueil', $this->get("router")->generate( '_homepage' ));
        $breadcrumbs->addItem( 'Profil de ' . $licensee[ "lastname" ] . " " . $licensee[ "firstname" ] );

        return $this->render('ApplicationSonataUserBundle:Default:dashboard.html.twig', array( "licensee" => $licensee,
                                                                                               "licenseeMatch_list" => $licenseeMatch_list,
                                                                                               "graphData" => $graphData,
                                                                                               "graphCategories" => $graphCategories,
                                                                                               "mensualPoint" => $mensualPoint,
                                                                                               "startSeasonMensualPoint" => $startSeasonMensualPoint,
                                                                                               "evoMensualPoint" => $evoMensualPoint
                                                                                             ));
    }
}